from django.shortcuts import render

# Create your views here.

from django.core.paginator import Paginator
from user.models import *
from utils.token import *

from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from rest_framework import status

from django.http import HttpResponse
from django.db.models import Q
from django.db.models import F

from utils.redis_middleware import redis_conn
from captcha.image import ImageCaptcha
import string
import random, logging
import json


# 图片验证码
class ImageView(APIView):

    def get(self, request, uuid):
        print("uuid>>>>>>>>>>", uuid, type(uuid))

        # 生成随机字符
        alpha_number = string.ascii_letters + string.digits
        img_code = "".join(random.sample(alpha_number, 4))
        print("image_code>>>>>>>>>>>>>>", img_code)

        # 生成图片验证码
        img_cap = ImageCaptcha()
        img_code_cap = img_cap.generate(img_code)
        print("img_code_cap>>>>>>>>>>>", img_code_cap)

        # 存储图片验证码
        key = "image_code:%s"%uuid
        red = redis_conn()
        red.set(key, img_code, ex=300)
        red.close()

        return HttpResponse(img_code_cap, content_type="image/png")


# 登录
class LoginView(APIView):

    def post(self, request):
        try:
            # 获取前端传递的数据
            account = request.data.get("account")
            password = request.data.get("password")
            code = request.data.get("code")
            uuid = request.data.get("uuid")
            print("接收", request.data)

            user = UserModel.objects.filter(account=account).first()
            print(user)
            # 校验
            red = redis_conn()
            key = "image_code:%s"%uuid
            red_code = red.get(key)
            print("red_code, code>>>>>>>>>>", red_code.decode().lower(), code)
            if red_code.decode().lower() != str(code).lower():
                return Response({"code": status.HTTP_400_BAD_REQUEST, "msg":"验证码不正确"})

            if user == None:
                return Response({"code": status.HTTP_400_BAD_REQUEST, "msg": "账号不存在"})

            if not user.password == password:
                return Response({"code": status.HTTP_400_BAD_REQUEST, "msg":"密码错误"})

            if user.is_delete == "1":
                return Response({"code": status.HTTP_400_BAD_REQUEST, "msg":"该用户已离职"})

            # 生成payload
            print("123456")
            payloads = {
                "id": user.id,
                "name": user.user_name,
                "role": user.role_id,
                "school": user.school_id,
            }
            data = {
                "id": user.id,
                "name":user.user_name
            }
            # ser = UserSer(user)

            token = login_token(payloads)
            return Response({"code": status.HTTP_200_OK, "msg":"登陆成功", "token": token, "data": data})
        except Exception as e:
            print("登陆的报错>>>>>>>>>>>", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"服务器错误"})




